﻿using System;

namespace DAPNet
{
    public class ClickDetectionStats
    {
        private ClickDetection detection;

        private ClickDetection perfectDetection;

        private SampleCollection clicked;

        private SampleCollection samples;

        private SampleCollection clicking;


        public ClickDetectionStats(ClickDetection detection, ClickDetection perfectDetection, SampleCollection clicked, SampleCollection samples, SampleCollection clicking)
        {
            if (detection.Count != perfectDetection.Count)
	        {
                // TODO: EXCEPTION CONSIDERATION.
		        throw new ArgumentException();
	        }

            this.detection = detection;
            this.perfectDetection = perfectDetection;
            this.samples = samples;
            this.clicked = clicked;
            this.clicking = clicking;
        }

        public int Count
        {
            get 
            { 
                return perfectDetection.Count; 
            }
        }

        public ClickDetection Detection
        {
            get
            {
                return detection;
            }
        }

        public ClickDetection PerfectDetection
        {
            get 
            { 
                return perfectDetection; 
            }
        }

        public SampleCollection Clicked
        {
            get
            {
                return clicked;
            }
        }

        public SampleCollection Samples
        {
            get
            {
                return samples;
            }
        }

        public SampleCollection Clicking
        {
            get
            {
                return clicking;
            }
        }

        public double GetFalsePositiveRatio()
        {
            return GetFalsePositiveRatio(0, Count - 1);
        }

        public double GetFalsePositiveRatio(int startIndex, int endIndex)
        {
            double numFalsePositives = GetNumDetections(false, true, startIndex, endIndex);
            double numPerfectNegatives = perfectDetection.GetNumDetections(false, startIndex, endIndex);
            double falsePositiveRatio = numFalsePositives / numPerfectNegatives;
            falsePositiveRatio = Double.IsNaN(falsePositiveRatio) ? 0 : falsePositiveRatio;
            return falsePositiveRatio;
        }

        public double GetMissedPositiveRatio()
        {
            return GetMissedPositiveRatio(0, Count - 1);
        }

        public double GetMissedPositiveRatio(int startIndex, int endIndex)
        {
            double numMissedPositives = GetNumDetections(true, false, startIndex, endIndex);
            double numPerfectPositives = perfectDetection.GetNumDetections(true, startIndex, endIndex);
            double missedPositiveRatio = numMissedPositives / numPerfectPositives;
            missedPositiveRatio = Double.IsNaN(missedPositiveRatio) ? 0 : missedPositiveRatio;
            return missedPositiveRatio;
        }

        public int GetNumDetections(bool isPerfectlyPositive, bool isPositive)
        {
            return GetNumDetections(isPerfectlyPositive, isPositive, 0, Count - 1);
        }

        public int GetNumDetections(bool isPerfectlyPositive, bool isPositive, int startIndex, int endIndex)
        {
            int numDetections = 0;
            for (int i = startIndex; i <= endIndex; i++)
            {
                if (perfectDetection[i] == isPerfectlyPositive &&
                    detection[i] == isPositive)
                {
                    numDetections++;
                } 
            }
            return numDetections;
        }
    }
}
